﻿using DreamCMS.BLL.Factory.Interface;
using DreamCMS.Entity;
using DreamCMS.Entity.Common;
using DreamCMS.WebCore.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.OleDb;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamCMS.DAL.DataAccessObject
{
    public class CommentDAO:ICommentDAO
    {
        /// <summary>
        /// 添加评论
        /// </summary>
        /// <param name="comment">评论实体</param>
        /// <returns></returns>
        public long InsertComment(CommentInfo comment)
        {
            string cmdText = @"insert into [dm_Comments](
                            PostId, ParentId,UserId,Name,Email,SiteUrl,Content,EmailNotify,IpAddress,CreateDate,Approved)
                             values (
                            @PostId, @ParentId,@UserId,@Name,@Email,@SiteUrl,@Content,@EmailNotify,@IpAddress,@CreateDate,@Approved)";

            OleDbParameter[] prams = { 
                                        AccessHelper.MakeParam("@PostId", OleDbType.Integer,8,System.Data.ParameterDirection.Input, comment.PostId),
                                        AccessHelper.MakeParam("@ParentId", OleDbType.Integer,8,ParameterDirection.Input, comment.ParentId),
                                        AccessHelper.MakeParam("@UserId", OleDbType.Integer,8,ParameterDirection.Input, comment.UserId),
                                        AccessHelper.MakeParam("@Name", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Name),
                                        AccessHelper.MakeParam("@Email", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Email),
                                        AccessHelper.MakeParam("@SiteUrl", OleDbType.VarWChar,255,ParameterDirection.Input, comment.SiteUrl),
                                        AccessHelper.MakeParam("@Content", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Content),
                                        AccessHelper.MakeParam("@EmailNotify", OleDbType.Integer,8 ,ParameterDirection.Input,    comment.EmailNotify),
                                        AccessHelper.MakeParam("@IpAddress", OleDbType.VarWChar,255,ParameterDirection.Input, comment.IpAddress),
                                        AccessHelper.MakeParam("@CreateDate", OleDbType.Date,8,ParameterDirection.Input, comment.CreateDate),
                                        AccessHelper.MakeParam("@Approved", OleDbType.Integer,8 ,ParameterDirection.Input,   comment.Approved),
            };
            AccessHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams);

            long newId = Convert.ToInt64(AccessHelper.ExecuteScalar(CommandType.Text,"select top 1 [CommentId] from [dm_Comments]  order by [CommentId] desc"));
            return newId;
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="comment"></param>
        /// <returns></returns>
        public long UpdateComment(CommentInfo comment)
        {
            string cmdText = @"update [dm_Comments] set 
                            PostId=@PostId,                            
                            ParentId=@ParentId,
                            UserId=@UserId,
                            Name=@Name,
                            Email=@Email,
                            SiteUrl=@SiteUrl,
                            Content=@Content,
                            EmailNotify=@EmailNotify,
                            IpAddress=@IpAddress,
                            CreateDate=@CreateDate,
                            Approved=@Approved
                            where CommentId=@CommentId ";

            OleDbParameter[] prams = { 
                                        AccessHelper.MakeParam("@PostId", OleDbType.Integer,8,ParameterDirection.Input, comment.PostId),
                                        AccessHelper.MakeParam("@ParentId", OleDbType.Integer,8,ParameterDirection.Input, comment.ParentId),
                                        AccessHelper.MakeParam("@UserId", OleDbType.Integer,8,ParameterDirection.Input, comment.UserId),
                                        AccessHelper.MakeParam("@Name", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Name),
                                        AccessHelper.MakeParam("@Email", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Email),
                                        AccessHelper.MakeParam("@SiteUrl", OleDbType.VarWChar,255,ParameterDirection.Input, comment.SiteUrl),
                                        AccessHelper.MakeParam("@Content", OleDbType.VarWChar,255,ParameterDirection.Input, comment.Content),
                                        AccessHelper.MakeParam("@EmailNotify", OleDbType.Integer,8 ,ParameterDirection.Input,    comment.EmailNotify),
                                        AccessHelper.MakeParam("@IpAddress", OleDbType.VarWChar,255,ParameterDirection.Input, comment.IpAddress),
                                        AccessHelper.MakeParam("@CreateDate", OleDbType.Date,8,ParameterDirection.Input, comment.CreateDate),
                                        AccessHelper.MakeParam("@Approved", OleDbType.Integer,8 ,ParameterDirection.Input,   comment.Approved),
                                        AccessHelper.MakeParam("@CommentId", OleDbType.Integer,8,ParameterDirection.Input, comment.CommentId),

                                    };
            return AccessHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams);

        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        public long DeleteComment(long commentId)
        {
            CommentInfo comment = GetComment(commentId);        //删除前

            string cmdText = "delete from [dm_Comments] where [commentId] = @commentId";
            OleDbParameter[] prams = { 
								AccessHelper.MakeParam("@commentId",OleDbType.Integer,8,ParameterDirection.Input,commentId)
							};

            long result = AccessHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams);
            return result;
        }

        /// <summary>
        /// 获取实体
        /// </summary>
        /// <param name="commentId"></param>
        /// <returns></returns>
        public CommentInfo GetComment(long commentId)
        {
            string cmdText = "select * from [dm_Comments] where [commentId] = @commentId";
            OleDbParameter[] prams = { 
								        AccessHelper.MakeParam("@commentId",OleDbType.Integer,8,ParameterDirection.Input,commentId)
							          };
            List<CommentInfo> list = DataSetToCommentList(AccessHelper.ExecuteDataSet(CommandType.Text,cmdText, prams));

            return list.Count > 0 ? list[0] : null;
        }


        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="postId"></param>
        /// <param name="pageSize"></param>
        /// <param name="pageIndex"></param>
        /// <param name="totalRecord"></param>
        /// <returns></returns>
        public List<CommentInfo> GetCommentList(long pageSize, long pageIndex, out long totalRecord, long order, long userId, long postId, long parentId, long approved, long emailNotify, string keyword)
        {
            string condition = " 1=1 ";// "[ParentId]=0 and [PostId]=" + postId;

            if (userId != -1)
            {
                condition += " and userid=" + userId;
            }
            if (postId != -1)
            {
                condition += " and postId=" + postId;
            }
            if (parentId != -1)
            {
                condition += " and parentId=" + parentId;
            }

            if (approved != -1)
            {
                condition += " and approved=" + approved;
            }

            if (emailNotify != -1)
            {
                condition += " and emailNotify=" + emailNotify;
            }

            if (!string.IsNullOrEmpty(keyword))
            {
                condition += string.Format(" and (content like '%{0}%' or author like '%{0}%' or ipaddress like '%{0}%' or email like '%{0}%'  or siteurl like '%{0}%' )", keyword);
            }

            string cmdTotalRecord = "select count(1) from [dm_Comments] where " + condition;
            totalRecord = Convert.ToInt64(AccessHelper.ExecuteScalar(CommandType.Text, cmdTotalRecord));

            //   throw new Exception(cmdTotalRecord);

            string cmdText = AccessHelper.GetPageSql("[dm_Comments]", "[CommentId]", "*", pageSize, pageIndex, order, condition);
            return DataSetToCommentList(AccessHelper.ExecuteDataSet(CommandType.Text, cmdText));
        }

        /// <summary>
        /// 根据日志ID删除评论
        /// </summary>
        /// <param name="postId">日志ID</param>
        /// <returns></returns>
        public long DeleteCommentByPost(long postId)
        {
            string cmdText = "delete from [dm_Comments] where [postId] = @postId";
            OleDbParameter[] prams = { 
								AccessHelper.MakeParam("@postId",OleDbType.Integer,8,ParameterDirection.Input,postId)
							};
            long result = AccessHelper.ExecuteNonQuery(CommandType.Text, cmdText, prams);

            return result;
        }

        /// <summary>
        /// 数据转换
        /// </summary>
        /// <param name="read"></param>
        /// <returns></returns>
        private List<CommentInfo> DataSetToCommentList(DataSet ds)
        {
            List<CommentInfo> list = new List<CommentInfo>();
            foreach(DataRow dr in ds.Tables[0].Rows)
            {
                CommentInfo comment = new CommentInfo();
                comment.CommentId = Convert.ToInt64(dr["CommentId"]);
                comment.ParentId = Convert.ToInt64(dr["ParentId"]);
                comment.PostId = Convert.ToInt64(dr["PostId"]);
                comment.UserId = Convert.ToInt64(dr["UserId"]);
                comment.Name = Convert.ToString(dr["Name"]);
                comment.Email = Convert.ToString(dr["Email"]);
                comment.SiteUrl = Convert.ToString(dr["SiteUrl"]);
                comment.Content = Convert.ToString(dr["Content"]);
                comment.EmailNotify = (Emailnotify)Convert.ToInt64(dr["EmailNotify"]);
                comment.IpAddress = Convert.ToString(dr["IpAddress"]);
                comment.CreateDate = Convert.ToDateTime(dr["CreateDate"]);
                comment.Approved = (CommentApprovedStatus)Convert.ToInt64(dr["Approved"]);
                list.Add(comment);
            }
            return list;
        }


        /// <summary>
        /// 统计评论
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="postId"></param>
        /// <param name="incChild"></param>
        /// <returns></returns>
        public long GetCommentCount(long userId, long postId, bool incChild)
        {
            string condition = " 1=1 ";
            if (userId != -1)
            {
                condition += " and [userId] = " + userId;
            }
            if (postId != -1)
            {
                condition += " and [postId] = " + postId;
            }
            if (incChild == false)
            {
                condition += " and [parentid]=0";
            }
            string cmdText = "select count(1) from [dm_Comments] where " + condition;
            return Convert.ToInt64(AccessHelper.ExecuteScalar(CommandType.Text, cmdText));
        }
    }
}
